package com.leapp.sk.view.im;

import android.app.Activity;
import android.graphics.Color;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.baselibrary.util.LKToastUtil;
import com.leapp.sk.R;
import com.leapp.sk.app.SKApplication;
import com.leapp.sk.util.im.LKVoiceRecorder;
import com.leapp.sk.util.im.listener.LKVoiceRecorderListener;

import java.io.File;
/**
 * 录音弹窗展示功能
 */
public class LKVoiceRecorderView implements LKVoiceRecorderListener {

    private static LKVoiceRecorderView mInstance = null;
    private PopupWindow mPopWin;
    private ImageView mDialogIcon;
    private ImageView mDialogVoice;
    private TextView mLable;
    private TextView mCountDown;
    private TextView mTvTime;
    private LKVoiceRecorder mVoiceRecorder;
    private Activity mActivity;

    private SKChatInputMenu.ChatInputMenuListener mchatInputMenuListener;

    public static LKVoiceRecorderView getInstance() {
        if (mInstance == null) {
            synchronized (LKVoiceRecorderView.class) {
                if (mInstance == null) {
                    mInstance = new LKVoiceRecorderView();
                }
            }
        }
        return mInstance;
    }


    /**
     * 显示窗口
     *
     * @param voiceView
     * @param activity
     */
    public void showVoiceView(TextView voiceView, Activity activity, SKChatInputMenu.ChatInputMenuListener mchatInputMenuListener) {
        mActivity=activity;
        this.mchatInputMenuListener=mchatInputMenuListener;
        closeView();
        View inflate = LayoutInflater.from(activity).inflate(R.layout.sk_voice_window_manager, null);
        mPopWin = new PopupWindow(inflate, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        mPopWin.showAtLocation(voiceView, Gravity.CENTER, 0, 0);
        mPopWin.setOutsideTouchable(false);
        mPopWin.setBackgroundDrawable(ContextCompat.getDrawable(activity,R.drawable.shape_record_shade));
        mCountDown =  inflate.findViewById(R.id.tv_counter);
        mDialogIcon =  inflate.findViewById(R.id.dialog_icon);
        mDialogVoice = inflate.findViewById(R.id.dialog_voice);
        mLable = inflate.findViewById(R.id.recorder_dialogtext);
        mTvTime =inflate.findViewById(R.id.tv_time);
        if (mPopWin != null && mPopWin.isShowing()) {
            mDialogIcon.setVisibility(View.VISIBLE);
            mDialogVoice.setVisibility(View.VISIBLE);
            mLable.setVisibility(View.VISIBLE);
            mDialogIcon.setImageResource(R.mipmap.recorder);
            mCountDown.setText("正在录音!..");
            mLable.setText("手指上滑,取消发送");
            voiceView.setText("松开 结束");
        }
        mVoiceRecorder = new LKVoiceRecorder(LKVoiceRecorderView.this);
        if (mVoiceRecorder != null) {
            mVoiceRecorder.startRecording();
        }
    }



    private static final int ampValue[] = {0, 1, 3, 5, 8, 12, 13};
    private static final int ampIcon[] = {R.mipmap.rcd_amp1, R.mipmap.rcd_amp2, R.mipmap.rcd_amp3, R.mipmap.rcd_amp4, R.mipmap.rcd_amp5, R.mipmap.rcd_amp6, R.mipmap.rcd_amp7};

    @Override
    public void noPermission() {
        LKToastUtil.showToastShort("请检查录音权限是否开启!");
       closeView();
    }

    @Override
    public void amplitude(final int amplitude) {
        SKApplication.getHandler().post(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < ampValue.length; i++) {
                    if (amplitude > ampValue[i] && amplitude <= ampValue[ampValue.length-1]) {
                        mDialogVoice.setBackgroundResource(ampIcon[i]);
                    }
                    if ((amplitude == -1) && (mPopWin != null)) {
                        mPopWin.dismiss();
                    }
                }
            }
        });
    }

    @Override
    public void voiceLength(final int voiceLength) {
        SKApplication.getHandler().post(new Runnable() {
            @Override
            public void run() {
                int time = 60 - voiceLength;
                if (time > 0) {
                    if ((60 - voiceLength) <= 10) {
                        mCountDown.setTextColor(Color.parseColor("#F06E6E"));
                    }
                    mCountDown.setText("录音倒计时：" + (60 - voiceLength));
                } else {
                    stopRecord();
                }
            }
        });
    }

    @Override
    public void stopRecording(int voiceLength, String voicePath, String voiceFileName, File
            voiceFile) {
        if (voiceLength <= 2) {
            mVoiceRecorder.deleteFile(voiceFile);
            timeIsShort();
            return;
        }
        closeView();
       if (mchatInputMenuListener!=null){
           mchatInputMenuListener.onSendVoice(voiceLength,voicePath,voiceFileName,voiceFile);
       }


    }

    @Override
    public void recordFail() {
        closeView();
    }

    /**
     * 关闭录音弹窗
     */
    private void closeView() {
        isCancel = false;
        if (mPopWin != null && mPopWin.isShowing()) {
            mPopWin.dismiss();
        }
    }


    private boolean isCancel = false;

    /**
     * 保持即将取消录音时控件状态
     *
     * @param view
     */
    public void cancelRecording(TextView view) {
        if (!isCancel) {
            isCancel = true;
            mDialogVoice.setVisibility(View.GONE);
            mDialogIcon.setImageResource(R.mipmap.rcd_cancel);
            mLable.setText("松开手指,取消发送");
            view.setText("松开手指,取消发送");
        }
    }

    /**
     * 删除并取消发送
     */
    public void cancelSend(TextView view) {
        closeView();
        mVoiceRecorder.discardRecording();
        view.setText("按住 说话");
    }

    /**
     * 保持录音中控件状态
     *
     * @param view
     */
    public void recording(TextView view) {
        if (isCancel) {
            isCancel = false;
            mDialogVoice.setVisibility(View.VISIBLE);
            mDialogIcon.setImageResource(R.mipmap.recorder);
            mLable.setText("手指上滑,取消发送");
            view.setText("松开 结束");
        }
    }

    /**
     * 停止录音
     */
    public void stopRecord() {
        if (mVoiceRecorder != null && mVoiceRecorder.isRecording()) {
            mVoiceRecorder.stopRecoding();
        }
    }

    /**
     * 录音时间太短
     */
    private void timeIsShort() {
        if (mPopWin != null && mPopWin.isShowing()) {
            mDialogIcon.setVisibility(View.VISIBLE);
            mDialogVoice.setVisibility(View.GONE);
            mLable.setVisibility(View.VISIBLE);
            mDialogIcon.setImageResource(R.mipmap.rcd_voice_to_short);
            mLable.setText("录音时间太短");
            SKApplication.getHandler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    closeView();
                }
            },500);

        }
    }
}
